package filetransfer;

import java.io.*;
import java.net.*;

public class FileSender
{
    public void SendFile(String FileName, String Destination, int Port) 
    {
	try
	{
	    // Ready socket for connection
	    Socket Target = new Socket(Destination, Port);
	    try
	    {
		// Prepare file to be sent
		File ToBeSent = new File(FileName);
		byte[] ByteMe = new byte[(int) ToBeSent.length()];
		
		FileInputStream FIS = new FileInputStream(ToBeSent);
		BufferedInputStream BIS = new BufferedInputStream(FIS);

		DataInputStream DIS = new DataInputStream(BIS);
		DIS.readFully(ByteMe,0,ByteMe.length);
		
		OutputStream OS = Target.getOutputStream();
		
		//Send Data to Server
		DataOutputStream DOS = new DataOutputStream(OS);
		DOS.writeUTF(ToBeSent.getName());
		DOS.writeLong(ByteMe.length);
		DOS.write(ByteMe,0,ByteMe.length);
		DOS.flush();
	    }
	    catch(FileNotFoundException NoFile)
	    {
		NoFile.printStackTrace();
	    }
	    finally
	    {
		Target.close();
	    }
	}
	catch (IOException IOEX)
	{
	    IOEX.printStackTrace();
	}
    }
}
